package com.gm;

import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.util.Scanner;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-12-24
 * Time: 下午2:44
 * To change this template use File | Settings | File Templates.
 */
public class ZipFrame extends JFrame{
    public ZipFrame() {
        setTitle("Java Zip");
        setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);

        JMenuBar menuBar=new JMenuBar();


        JMenu fileMenu=new JMenu("File");

        JMenuItem fileOpenMenu=new JMenuItem("Open");
        fileOpenMenu.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser chooser=new JFileChooser(new File("."));
                chooser.setFileFilter(new FileNameExtensionFilter("Zip File","zip"));
                if(chooser.showOpenDialog(ZipFrame.this)==JFileChooser.APPROVE_OPTION)
                {
                    zipName=chooser.getSelectedFile().getPath();
                    fileCombox.removeAllItems();
                    LoadZipList();
                }
            }
        });
        fileMenu.add(fileOpenMenu);

        JMenuItem fileExitMenu=new JMenuItem("Exit");
        fileExitMenu.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        menuBar.add(fileMenu);
        setJMenuBar(menuBar);

        fileCombox=new JComboBox<String>();
        fileCombox.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                LoadZipFile((String)fileCombox.getSelectedItem());
            }
        });
        fileContent=new JTextArea();

        add(new JScrollPane(fileContent), BorderLayout.CENTER);
        add(fileCombox,BorderLayout.SOUTH);

    }

    private void LoadZipFile(final String selectedItem) {
        fileCombox.setEnabled(false);
        fileContent.setText("");

        new SwingWorker<Void,String>(){

            @Override
            protected Void doInBackground() throws Exception {
                ZipInputStream zipInputStream=new ZipInputStream(new FileInputStream(zipName));
                ZipEntry zipEntry;

                while((zipEntry=zipInputStream.getNextEntry())!=null){
                    if(zipEntry.getName().equals(selectedItem))
                    {
                        Scanner scanner=new Scanner(zipInputStream);
                        while(scanner.hasNextLine()){
                            fileContent.append(scanner.nextLine());
                            fileContent.append("\n");
                        }
                        scanner.close();
                    }
                    zipInputStream.closeEntry();
                }

                zipInputStream.close();;
                return null;
            }

            protected void done()
            {
                fileCombox.setEnabled(true);
            }
        }.execute();
    }

    public void LoadZipList(){
        new SwingWorker<Void,String>(){

            @Override
            protected Void doInBackground() throws Exception {
                ZipInputStream zipInputStream=new ZipInputStream(new FileInputStream(zipName));
                ZipEntry zipEntry;

                while((zipEntry=zipInputStream.getNextEntry())!=null){
                    publish(zipEntry.getName());
                    zipInputStream.closeEntry();
                }

                zipInputStream.close();;
                return null;
            }

            protected void process(List<String> zipNames){
                for(String name: zipNames){
                    fileCombox.addItem(name);
                }
            }
        }.execute();
    }
    private final int DEFAULT_HEIGHT=400;
    private final int DEFAULT_WIDTH=400;
    private JComboBox<String> fileCombox;
    private JTextArea fileContent;
    private String zipName;
}
